package com.newlink.building.common_base.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.SpannableString
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import com.newlink.building.common_base.R
import com.newlink.building.common_base.databinding.DialogMessageCustomBinding


/**
 * @author wangyang
 * @date 2022/10/26
 * @description 自定义信息描述弹窗（标题、信息描述、确定、取消）
 */
class CustomMessageDialog(context: Context) : Dialog(context, R.style.toast_setting_dialog) {

    private var mTitle: String = ""
    private var mMessage: String = ""
    private lateinit var mMessageSpannableString: SpannableString
    private var mMessageHint: String = ""
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null

    private lateinit var mBinding: DialogMessageCustomBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DialogMessageCustomBinding.inflate(LayoutInflater.from(context))
        setContentView(mBinding.root)
        mBinding.tvTitleCustomDialog.text = mTitle
        if (TextUtils.isEmpty(mMessage)) {
            mBinding.tvDescribeCustomDialog.text = mMessageSpannableString
        } else {
            mBinding.tvDescribeCustomDialog.text = mMessage
        }
        mBinding.tvDescribeHintCustomDialog.text = mMessageHint
        if (!TextUtils.isEmpty(mMessageHint)) {
            mBinding.tvDescribeHintCustomDialog.visibility = View.VISIBLE
        } else {
            mBinding.tvDescribeHintCustomDialog.visibility = View.INVISIBLE
        }
        if (TextUtils.isEmpty(mPositive)) {
            mBinding.msgBtnConfirm.visibility = View.GONE
            mBinding.msgBtnSpace.visibility = View.GONE
        } else {
            mBinding.msgBtnConfirm.text = mPositive
            mBinding.msgBtnConfirm.setOnClickListener {
                positiveListener?.onClick(this)
            }
        }

        if (TextUtils.isEmpty(mNegative)) {
            mBinding.msgBtnCancel.visibility = View.GONE
            mBinding.msgBtnSpace.visibility = View.GONE
        } else {
            mBinding.msgBtnCancel.text = mNegative
            mBinding.msgBtnCancel.setOnClickListener {
                negativeListener?.onClick(this)
            }
        }


    }

    fun setTitle(title: String): CustomMessageDialog {
        mTitle = title
        return this
    }

    fun setMessage(message: String): CustomMessageDialog {
        mMessage = message
        return this
    }

    fun setMessage(message: SpannableString): CustomMessageDialog {
        mMessageSpannableString = message
        return this
    }

    fun setMessageHint(messageHint: String): CustomMessageDialog {
        mMessageHint = messageHint
        return this
    }

    fun setNegativeButton(
        listener: OnClickListener?
    ): CustomMessageDialog {
        if (listener == null) {
            this.mNegative = ""
        } else {
            this.mNegative = context.resources.getString(R.string.cancel_dialog_custom)
            this.negativeListener = listener
        }
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: OnClickListener
    ): CustomMessageDialog {
        if (TextUtils.isEmpty(text)) {
            this.mNegative = ""
        } else {
            this.mNegative = text
            this.negativeListener = listener
        }

        return this
    }

    fun setPositiveButton(
        listener: OnClickListener?
    ): CustomMessageDialog {
        if (listener == null) {
            this.mPositive = ""
        } else {
            this.mPositive = context.resources.getString(R.string.confirm_dialog_custom)
            this.positiveListener = listener
        }
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.confirm_dialog_custom),
        listener: OnClickListener
    ): CustomMessageDialog {
        if (TextUtils.isEmpty(text)) {
            this.mPositive = ""
        } else {
            this.mPositive = text
            this.positiveListener = listener
        }

        return this
    }

    fun interface OnClickListener {
        fun onClick(dialog: CustomMessageDialog)
    }
}